﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace StudentVideo.Data.Migrations
{
    /// <inheritdoc />
    public partial class updateDatabase1 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterDatabase()
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "course",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Code = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "编码")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "启用状态"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_course", x => x.Id);
                },
                comment: "课程")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "major",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Code = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "编码")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "激活状态"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_major", x => x.Id);
                },
                comment: "专业")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "permission",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "显示名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Code = table.Column<string>(type: "varchar(150)", maxLength: 150, nullable: false, comment: "唯一标识")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Description = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: true, comment: "描述")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否启用"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_permission", x => x.Id);
                },
                comment: "权限配置")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "role",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Name = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false, comment: "角色名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否启用"),
                    Type = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "角色类型"),
                    Description = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: true, comment: "描述")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role", x => x.Id);
                },
                comment: "角色表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "student",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    StudentId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "学号")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IdCard = table.Column<string>(type: "varchar(32)", maxLength: 32, nullable: false, comment: "身份证")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Password = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "密码")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Salt = table.Column<byte[]>(type: "longblob", nullable: false, comment: "密码加密段"),
                    Grade = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "学年"),
                    Semester = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "学期"),
                    Major = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "专业")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Gender = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "性别"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "启用状态"),
                    Name = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false, comment: "姓名")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Email = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "邮箱")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Phone = table.Column<string>(type: "varchar(11)", maxLength: 11, nullable: false, comment: "联系电话")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    BirthDate = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "出生日期"),
                    Address = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true, comment: "家庭住址")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    EnrollmentDate = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "入学日期"),
                    GraduationDate = table.Column<DateTime>(type: "datetime(6)", nullable: true, comment: "毕业日期"),
                    ContactName = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: false, comment: "紧急联系人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ContactPhone = table.Column<string>(type: "varchar(11)", maxLength: 11, nullable: false, comment: "紧急联系人电话")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_student", x => x.Id);
                },
                comment: "学生表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "system_menu",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "菜单名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Path = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false, comment: "菜单路径")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Icon = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: true, comment: "菜单图标")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ParentId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "父菜单 id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否启用"),
                    Sort = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "排序 - 同父级下的顺序"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_system_menu", x => x.Id);
                },
                comment: "管理系统菜单")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "user",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Username = table.Column<string>(type: "varchar(15)", maxLength: 15, nullable: false, comment: "用户名")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Nickname = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: true, comment: "昵称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Password = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "密码")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Gender = table.Column<sbyte>(type: "tinyint", nullable: false, comment: "性别"),
                    Enable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "启用状态"),
                    Salt = table.Column<byte[]>(type: "longblob", nullable: false, comment: "密码加密段"),
                    Avatar = table.Column<string>(type: "varchar(150)", maxLength: 150, nullable: true, comment: "头像")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user", x => x.Id);
                },
                comment: "管理员用户")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "video",
                columns: table => new
                {
                    Id = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "唯一id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CourseId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "关联课程")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Chapter = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false, comment: "章节")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Summary = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false, comment: "小节")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Title = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false, comment: "标题")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Url = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "地址")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Video = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "本地地址(id)")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "上次修改人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    UpdateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "上次更新时间"),
                    CreateBy = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "创建人")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreateAt = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_video", x => x.Id);
                    table.ForeignKey(
                        name: "FK_video_course_CourseId",
                        column: x => x.CourseId,
                        principalTable: "course",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "课程视频")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "major_course_map",
                columns: table => new
                {
                    MajorId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "专业id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CourseId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "课程id")
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_major_course_map", x => new { x.MajorId, x.CourseId });
                    table.ForeignKey(
                        name: "FK_major_course_map_course_CourseId",
                        column: x => x.CourseId,
                        principalTable: "course",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_major_course_map_major_MajorId",
                        column: x => x.MajorId,
                        principalTable: "major",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "专业-课程映射")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "role_permission_map",
                columns: table => new
                {
                    RoleId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "角色 id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    PermissionId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "权限 id")
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role_permission_map", x => new { x.RoleId, x.PermissionId });
                    table.ForeignKey(
                        name: "FK_role_permission_map_permission_PermissionId",
                        column: x => x.PermissionId,
                        principalTable: "permission",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_role_permission_map_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "角色 - 权限 映射表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "user_role_map",
                columns: table => new
                {
                    UserId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "用户 id")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    RoleId = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false, comment: "角色 id")
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_role_map", x => new { x.UserId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_user_role_map_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_user_role_map_user_UserId",
                        column: x => x.UserId,
                        principalTable: "user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "用户 - 角色 映射表")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_course_Code",
                table: "course",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_major_course_map_CourseId",
                table: "major_course_map",
                column: "CourseId");

            migrationBuilder.CreateIndex(
                name: "IX_major_course_map_MajorId",
                table: "major_course_map",
                column: "MajorId");

            migrationBuilder.CreateIndex(
                name: "IX_permission_Code",
                table: "permission",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_role_permission_map_PermissionId",
                table: "role_permission_map",
                column: "PermissionId");

            migrationBuilder.CreateIndex(
                name: "IX_role_permission_map_RoleId",
                table: "role_permission_map",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_student_IdCard",
                table: "student",
                column: "IdCard",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_student_StudentId",
                table: "student",
                column: "StudentId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_system_menu_Path",
                table: "system_menu",
                column: "Path",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_role_map_RoleId",
                table: "user_role_map",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_user_role_map_UserId",
                table: "user_role_map",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_video_CourseId",
                table: "video",
                column: "CourseId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "major_course_map");

            migrationBuilder.DropTable(
                name: "role_permission_map");

            migrationBuilder.DropTable(
                name: "student");

            migrationBuilder.DropTable(
                name: "system_menu");

            migrationBuilder.DropTable(
                name: "user_role_map");

            migrationBuilder.DropTable(
                name: "video");

            migrationBuilder.DropTable(
                name: "major");

            migrationBuilder.DropTable(
                name: "permission");

            migrationBuilder.DropTable(
                name: "role");

            migrationBuilder.DropTable(
                name: "user");

            migrationBuilder.DropTable(
                name: "course");
        }
    }
}
